package cn.clone;

public class LightClone {

    public static void main(String[] args) {
        Student student = new Student();
        Address add = new Address();
        add.setName("杭州");
        student.setName("xiaokang");
        student.setId(1);
        student.setAddress(add);
        Student student1;
        student1 = (Student)student.clone();
        add.setName("西湖");
        student1.setName("xiao");
        student1.setId(2);
        System.out.println(student);
        System.out.println(student1);
    }

}

class Address implements Cloneable{
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    protected Object clone(){
        Address address = null;
        try {
            address = (Address) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return address;
    }
}

class Student implements Cloneable{

    private String name;
    private int id;
    private Address address;

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    protected Object clone(){
        Student stu = null;
        try {
            stu = (Student) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        stu.address = (Address) address.clone();
        return stu;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", id=" + id +
                ", address=" + address.getName() +
                '}';
    }
}
